package com.test.pack1.demo.d2;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

/**
 * Created by dws on 2017/11/13.
 * 链表实现背包
 */
public class Bag<Item> implements  Iterable<Item>{

    private Node first;
    public void add(Item item){
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }

    @NotNull
    @Override
    public Iterator<Item> iterator() {
        return new ListIterator();
    }
    private class ListIterator implements Iterator<Item>{

        private Node current = first;

        @Override
        public boolean hasNext() {
            return current==null;
        }

        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }

        @Override
        public void remove() {

        }
    }

    private class Node{
        Item item;
        Node next;
    }


}
